<?php
class HomeController extends Controller {
    
    public function actions() {
        return array(
            'sitemap'=>array(
                'class'=>'ext.sitemap.ESitemapAction',
                'importListMethod'=>'getBaseSitePageList',
                'classConfig'=>array(
                    array('baseModel'=>'Articles',
                          'route'=>'article/index',
                          'params'=>array('id'=>'id'),
                    ),         
                ),
            ),
            'sitemapxml'=>array(
                'class'=>'ext.sitemap.ESitemapXMLAction',
                'classConfig'=>array(
                    array('baseModel'=>'Articles',
                          'route'=>'article/index',
                          'params'=>array('id'=>'id'),
                          'frequency' => 'weekly',
                          'priority' => '0.8'
                    ),         
                ),
                //'bypassLogs'=>true, // if using yii debug toolbar enable this line
                'importListMethod'=>'getBaseSitePageList',
            ),          
        );
    }    
    
    public function getBaseSitePageList() {
        $list = array(
                    array(
                        'loc'=>Yii::app()->createAbsoluteUrl('/'),
                        'frequency'=>'weekly',
                        'priority'=>'1',
                    ),
                    array(
                        'loc'=>Yii::app()->createAbsoluteUrl('pages/about'),
                        'frequency'=>'weekly',
                        'priority'=>'0.8',
                    ),
                    array(
                        'loc'=>Yii::app()->createAbsoluteUrl('pages/contact'),
                        'frequency'=>'weekly',
                        'priority'=>'0.8',
                    ),                    
                    array(
                        'loc'=>Yii::app()->createAbsoluteUrl('pages/privacy'),
                        'frequency'=>'weekly',
                        'priority'=>'0.8',
                    ),

                );
        return $list;
    }    
    
    
	public function actionIndex() {
		$this->pageTitle= Yii::t('app', 'Главная');
		$this->logPageView();
		if (!$this->checkCookie('region_id')) {
			$new_firms = array();
		} else {
			$region_id = intval($this->getCookie('region_id'));
			$city_id = intval($this->getCookie('city_id'));
			$criteria = new CDbCriteria();
			$criteria->compare('t.country_id', $this->getYiiP('default_country_id'));
			
			if (0 < $region_id) $criteria->compare('t.region_id', $region_id);
			if (0 < $city_id) $criteria->compare('t.city_id', $city_id);
			
			//$new_firms = Firms::model()->actual()->latest(4)->with('firmTmpl', 'rubric', 'category', 'country', 'region', 'city')->findAll($criteria);
			$new_firms = Firms::model()->actual()->latest(4)->findAll($criteria);
		}
    //$block_nine = $this->getBlockNineContent();
    $block_nine = "";    
		$this->render('index', array('new_firms' => $new_firms, 'block_nine' => $block_nine));
	}
	
    protected function getBlockNineContent() {
        $vip_tmpl_id = 3;
        $paid_tmpl_id = 2;
		
		if (!$this->checkCookie('region_id')) return '';
		
		$region_id = intval($this->getCookie('region_id'));
		$city_id = intval($this->getCookie('city_id'));
		
		$criteria = new CDbCriteria();
		$criteria->compare('t.country_id', $this->getYiiP('default_country_id'));
		
		if (0 < $region_id) $criteria->compare('t.region_id', $region_id);
		if (0 < $city_id) $criteria->compare('t.city_id', $city_id);
		
		
        $vip_data = Firms::model()->actual()
			//->with('firmTmpl', 'rubric', 'category', 'country', 'region', 'city')
			->firm_tmpl($vip_tmpl_id)->random(9)->findAll($criteria);
		
        $paid_data = array();
        $vip_found = count($vip_data);
        $adv_left = 0;
        if (9 > count($vip_found)) {
            $paid_data = Firms::model()->actual()
				//->with('firmTmpl', 'rubric', 'category', 'country', 'region', 'city')
				->firm_tmpl($paid_tmpl_id)->random(9-$vip_found)->findAll($criteria);
        }
        $records = array();
        foreach($vip_data as $vip) {
            $records[] = $vip;
        }
        
        foreach($paid_data as $paid) {
            $records[] = $paid;
        }
        $adv_left = 9 - count($records);
        return $this->renderPartial('block_nine', array('records' => $records, 'adv_left' => $adv_left), true);
    }
    
	/**
	 * This is the action to handle external exceptions.
	 */
	public function actionError() {
		$error = Yii::app()->errorHandler->error;
	    if ($error) {
	    	if(Yii::app()->request->isAjaxRequest)
	    		echo $error['message'];
	    	else
	        	$this->render('error', $error);
	    }
	}	
	
	/**
	 * Displays the login page
	 */
	public function actionLogin() {
		$this->pageTitle= Yii::t('app', 'Логин');
		$this->logPageView();
		$model=new LoginForm;
		if(isset($_POST['ajax']) && $_POST['ajax']==='login-form') {
			echo CActiveForm::validate($model);
			Yii::app()->end();
		}

		if(isset($_POST['LoginForm'])) {
			$model->attributes=$_POST['LoginForm'];
			$model->username = trim($model->username);
			$model->password = trim($model->password);
			if($model->validate() && $model->login()) {
				$url = (Controller::checkAdmin()) ? $this->createUrl('admin/default/index') : Yii::app()->homeUrl;
				$this->redirect($url);
			}	
		}
		$this->render('login',array('model'=>$model));
	}

	/**
	 * Logs out the current user and redirect to homepage.
	 */
	public function actionLogout() {
		Yii::app()->user->logout();
		$this->redirect(Yii::app()->homeUrl);
	}	
}